package com.ruoyi.agriculture.zs.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.agriculture.zs.domain.ZsQuestion;
import com.ruoyi.agriculture.zs.mapper.ZsQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.agriculture.zs.mapper.ZsQuestionCommentMapper;
import com.ruoyi.agriculture.zs.domain.ZsQuestionComment;
import com.ruoyi.agriculture.zs.service.IZsQuestionCommentService;

/**
 * 评论Service业务层处理
 *
 * @author zs
 * @date 2025-07-27
 */
@Service
public class ZsQuestionCommentServiceImpl implements IZsQuestionCommentService
{
    @Autowired
    private ZsQuestionCommentMapper zsQuestionCommentMapper;

    @Autowired
    private ZsQuestionMapper zsQuestionMapper;

    /**
     * 查询评论
     *
     * @param zsCommentId 评论主键
     * @return 评论
     */
    @Override
    public ZsQuestionComment selectZsQuestionCommentByZsCommentId(Long zsCommentId)
    {
        return zsQuestionCommentMapper.selectZsQuestionCommentByZsCommentId(zsCommentId);
    }

    /**
     * 查询评论列表
     *
     * @param zsQuestionComment 评论
     * @return 评论
     */
    @Override
    public List<ZsQuestionComment> selectZsQuestionCommentList(ZsQuestionComment zsQuestionComment)
    {
        return zsQuestionCommentMapper.selectZsQuestionCommentList(zsQuestionComment);
    }

    /**
     * 新增评论
     *
     * @param zsQuestionComment 评论
     * @return 结果
     */
    @Override
    public int insertZsQuestionComment(ZsQuestionComment zsQuestionComment)
    {

        ZsQuestion zsQuestion = zsQuestionMapper.selectZsQuestionByZsQuestionId(zsQuestionComment.getZsQuestionId());
        if(zsQuestion==null){
            throw new RuntimeException("评论对象为空！");
        }
        zsQuestion.setZsQuestionCommentNum(zsQuestion.getZsQuestionCommentNum() + 1);

        zsQuestionMapper.updateZsQuestion(zsQuestion);

        zsQuestionComment.setDeptId(SecurityUtils.getLoginUser().getDeptId());
        zsQuestionComment.setUserId(SecurityUtils.getUserId());
        zsQuestionComment.setUserName(SecurityUtils.getUsername());
        zsQuestionComment.setCreateTime(DateUtils.getNowDate());
        return zsQuestionCommentMapper.insertZsQuestionComment(zsQuestionComment);
    }

    /**
     * 修改评论
     *
     * @param zsQuestionComment 评论
     * @return 结果
     */
    @Override
    public int updateZsQuestionComment(ZsQuestionComment zsQuestionComment)
    {
        zsQuestionComment.setUpdateTime(DateUtils.getNowDate());
        return zsQuestionCommentMapper.updateZsQuestionComment(zsQuestionComment);
    }

    /**
     * 批量删除评论
     *
     * @param zsCommentIds 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteZsQuestionCommentByZsCommentIds(Long[] zsCommentIds)
    {
        return zsQuestionCommentMapper.deleteZsQuestionCommentByZsCommentIds(zsCommentIds);
    }

    /**
     * 删除评论信息
     *
     * @param zsCommentId 评论主键
     * @return 结果
     */
    @Override
    public int deleteZsQuestionCommentByZsCommentId(Long zsCommentId)
    {
        return zsQuestionCommentMapper.deleteZsQuestionCommentByZsCommentId(zsCommentId);
    }
}
